﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using SquidConsult.BLL;
using System.Data.SqlClient;
using System.ServiceModel;

namespace SquidConsult.UI.Consultant
{
  public partial class _default : System.Web.UI.Page
  {
      private ConsultantBL consultant = new ConsultantBL();
      System.Guid consultantID;

      #region Load page 
      protected void Page_Load(object sender, EventArgs e)
      {
          MembershipUser userID = Membership.GetUser();
          string conId = userID.ProviderUserKey.ToString();
          consultantID = System.Guid.Parse(conId);

          loadSupervisorName();
      }

      //Add department supervisorName into  SupDropDownList according to login id
      private void loadSupervisorName()
      {
          if (!IsPostBack)
          {
              SupDropDownList.DataSource = consultant.GetSupervisorName(consultantID);
              SupDropDownList.DataBind();
          }  
      }
      #endregion

      #region Convert Currency
      protected void AmountTextBox_TextChanged(object sender, EventArgs e)
    {
        changeCurrency();
    }

    protected void CurrencyDropDownList_SelectedIndexChanged(object sender, EventArgs e)
    {
        changeCurrency();
    }

    private void changeCurrency()
    {
        double amount = 0;
        try
        {
            if (double.TryParse(AmountTextBox.Text, out amount) && amount > 0)
            {
                CurrencyLabel.Text = consultant.GetCurrency(amount, CurrencyDropDownList.SelectedValue).ToString();
                ErrorLabel.Text = "";
            }
            else
                ErrorLabel.Text = "You must enter a valid positive number, eg.123.";
        }
        catch (CommunicationException)
        {
            ErrorLabel.Text = "Please check your internet connection.";
        }
    }
     #endregion 

      #region Add Expense
    protected void AddButton_Click(object sender, EventArgs e)
    {
        if (DateTextBox.Text != "" && LocationTextBox.Text != "" && DescTextBox.Text != "" && AmountTextBox.Text != "")
        {
            if (ReceiptFileUpload.HasFile)
            {
                try
                {
                    if (ReceiptFileUpload.PostedFile.FileName.EndsWith("pdf", true, null))
                    {
                        string filePath = Server.MapPath("~/Receipts/" + ReceiptFileUpload.FileName);
                        ReceiptFileUpload.SaveAs(filePath);
                        consultant.SetExpense(System.DateTime.Parse(DateTextBox.Text), LocationTextBox.Text, DescTextBox.Text, CurrencyLabel.Text, filePath);
                        MessageLabel.Text = "Date: " + DateTextBox.Text + " Amount: " + AmountTextBox.Text + " expense added!";
                        Clear();
                    }
                    else
                        MessageLabel.Text = "An expense add failed: Only PDF files are accepted!";
                }
                catch (Exception ex)
                {
                    MessageLabel.Text = "An expense add failed: The following error occured: " + ex.Message;
                }
            }
            else MessageLabel.Text = "Please Upload a valid PDF file"; 
        }
        else
            MessageLabel.Text = "An expense add failed: Please fill all blanks!";
    }

    private void Clear()
    {
        DateTextBox.Text = "";
        LocationTextBox.Text = "";
        DescTextBox.Text = "";
        AmountTextBox.Text = "";
        CurrencyLabel.Text = "";
        SubmitLabel.Text = "";
    }
    #endregion 

      #region Add Report

    protected void SubmitButton_Click(object sender, EventArgs e)
    {
        try
        {
            consultant.SetReport(consultantID, SupDropDownList.SelectedValue.ToString());
            SubmitLabel.Text = "Submit report succeeded!";
            MessageLabel.Text = "";
            consultant.GenerateReportID = true;
        }
        catch (Exception ex)
        {
            SubmitLabel.Text = "A report submit fail : The following error occured: " + ex.Message;
        }
    }
    #endregion 
  }
}